package com.mingqijia.gassafety.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gap.cache.redis.RedisCache;
import com.mingqijia.gassafety.db.entity.ConsumerServiceInfo;
import com.mingqijia.gassafety.db.entity.Dtu;
import com.mingqijia.gassafety.db.mapper.ConsumerServiceInfoMapper;
import com.mingqijia.gassafety.job.service.ConsumerServiceInfoService;
import com.mingqijia.gassafety.job.service.EquipmentService;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.constant.EquipmentType;
import com.mingqijia.gassafety.shared.enums.YesNoEnum;
import com.mingqijia.gassafety.shared.utils.CompareUtil;
import com.mingqijia.gassafety.shared.utils.commonUtils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.*;

/**
 * <p>
 * 服务信息表 服务实现类
 * </p>
 *
 * @author mby
 * @since 2024-01-12
 */
@Service
@Slf4j
public class ConsumerServiceInfoServiceImpl extends ServiceImpl<ConsumerServiceInfoMapper, ConsumerServiceInfo> implements ConsumerServiceInfoService {

    @Autowired
    EquipmentService equipmentService;
    @Autowired
    ConsumerServiceInfoMapper consumerServiceInfoMapper;
    @Autowired
    RedisCache redisCache;

    @Override
    @Transactional
    public void addConsumerServiceInfo(String imei, String industryType) {
        LambdaQueryWrapper<Dtu> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Dtu::getIMEI,imei).eq(Dtu::getIsDeleted, Constants.IS_DELETED_FALSE);
        Dtu dtu = equipmentService.getOne(queryWrapper);
        ConsumerServiceInfo service = new ConsumerServiceInfo();
        service.setId(null);
        service.setConsumerId(dtu.getConsumerId());
        service.setIndustryType(industryType);
        service.setImei(dtu.getIMEI());
        try {
            service.setStartDate(DateUtils.toDate(dtu.getInstallDate()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        try {
            if (EquipmentType.HOME_GAS_DETECTOR.getCode().equals(dtu.getEquipmentType())){
                service.setEndDate(DateUtils.getDiffYearDate(DateUtils.toDate(dtu.getInstallDate()), 5));
            } else {
                // 工商业报警器
                service.setEndDate(DateUtils.getDiffYearDate(DateUtils.toDate(dtu.getInstallDate()), 3));
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        service.setRemark(new StringBuilder().append("设备号：").append(dtu.getIMEI())
                .append("安装日期：").append(dtu.getInstallDate()).append("当前状态：关联").toString());
        service.setIsDeleted(YesNoEnum.N.isFlag());
        service.setCreatedAt(new Date());
        service.setUpdatedAt(service.getCreatedAt());
        service.setSpId(dtu.getSpId());
        service.setIsUpdate(1);
        service.setIsSend(0);
        consumerServiceInfoMapper.insert(service);
    }

    @Override
    @Transactional
    public void addConsumerServiceInfo(String imei, String oldImei, String industryType) {
        LambdaQueryWrapper<Dtu> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Dtu::getIMEI,imei).eq(Dtu::getIsDeleted, Constants.IS_DELETED_FALSE);
        Dtu dtu = equipmentService.getOne(queryWrapper);
        LambdaQueryWrapper<ConsumerServiceInfo> serviceInfoQueryWrapper = new LambdaQueryWrapper();
        serviceInfoQueryWrapper.eq(ConsumerServiceInfo::getImei,oldImei).eq(ConsumerServiceInfo::getIsDeleted, Constants.IS_DELETED_FALSE);
        ConsumerServiceInfo serviceInfo = this.getOne(serviceInfoQueryWrapper);
        ConsumerServiceInfo service = new ConsumerServiceInfo();
        service.setId(null);
        service.setConsumerId(dtu.getConsumerId());
        service.setIndustryType(industryType);
        service.setImei(dtu.getIMEI());
        service.setStartDate(serviceInfo.getStartDate());
        service.setEndDate(serviceInfo.getEndDate());
        service.setRemark(new StringBuilder().append("设备号：").append(dtu.getIMEI())
                .append("安装日期：").append(dtu.getInstallDate()).append("当前状态：关联").toString());
        service.setIsDeleted(YesNoEnum.N.isFlag());
        service.setCreatedAt(new Date());
        service.setUpdatedAt(service.getCreatedAt());
        service.setSpId(dtu.getSpId());
        service.setIsUpdate(1);
        service.setIsSend(0);
        consumerServiceInfoMapper.insert(service);
    }

    @Override
    @Transactional
    public void deleteConsumerServiceInfo(String imei) {
        LambdaUpdateWrapper<ConsumerServiceInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ConsumerServiceInfo::getImei, imei)
                .set(ConsumerServiceInfo::getIsDeleted, YesNoEnum.Y.isFlag());
        this.update(updateWrapper);
        // 删除缓存
        redisCache.deleteObject(Constants.WARNING_SERVICE_PERIOD_RUNNING + ":" + imei);
    }


    @Override
    @Transactional
    public void editConsumerServiceInfoIsSend(String imei) {
        LambdaUpdateWrapper<ConsumerServiceInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ConsumerServiceInfo::getImei, imei)
                .eq(ConsumerServiceInfo::getIsDeleted, YesNoEnum.N.isFlag())
                .set(ConsumerServiceInfo::getIsSend,1);
        this.update(updateWrapper);
    }

}
